package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.Detailed_Travel_area_Activity;
import com.itcast.donkey.modles.TravelAreaModles;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by lanouhn on 16/8/25.
 */
public class TravelAreaAdapter extends RecyclerView.Adapter<TravelAreaAdapter.AreaViewHolder> {

    private Context context;
    private ArrayList<TravelAreaModles> modlesArrayList;
    private int id;

    public TravelAreaAdapter(Context context, ArrayList<TravelAreaModles> modlesArrayList,int id) {
        this.context = context;
        this.modlesArrayList = modlesArrayList;
        this.id=id;
    }

    @Override
    public AreaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_activity_area_travel, null);
        AreaViewHolder holder = new AreaViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(AreaViewHolder holder, final int position) {
        Picasso.with(context).load(modlesArrayList.get(position).getImage_url()).into(holder.ivItemAreaBg);
        holder.tvAreaTrip.setText(modlesArrayList.get(position).getAttraction_trips_count() + "篇游记");
        holder.tvItemAreaName.setText(modlesArrayList.get(position).getName());
        holder.tvItemAreaContent.setText(modlesArrayList.get(position).getDescription());
        String amount = modlesArrayList.get(position).getUser_score();

        if (amount != null) {
            double am = Double.valueOf(amount);
            if (am < 4.5) {
                holder.ivItemAreaStar.setBackgroundResource(R.mipmap.star_24_empty);
            } else if (am >= 4.5) {
                holder.ivItemAreaStar.setBackgroundResource(R.mipmap.star_24_half);
            } else {
                holder.ivItemAreaStar.setBackgroundResource(R.mipmap.star_24_half);
            }
        } else {
            holder.ivItemAreaStar.setBackgroundResource(R.mipmap.star_24_empty);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detailed_Travel_area_Activity.class);
                intent.putExtra("id", modlesArrayList.get(position).getId());
                intent.putExtra("mapId",id);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return modlesArrayList.size();
    }

    public class AreaViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivItemAreaBg;
        private TextView tvAreaTrip;
        private TextView tvItemAreaName;
        private ImageView ivItemAreaStar;
        private TextView tvItemAreaContent;

        public AreaViewHolder(View itemView) {
            super(itemView);
            ivItemAreaBg = (ImageView) itemView.findViewById(R.id.iv_item_area_bg);
            tvAreaTrip = (TextView) itemView.findViewById(R.id.tv_area_trip);
            tvItemAreaName = (TextView) itemView.findViewById(R.id.tv_item_area_name);
            ivItemAreaStar = (ImageView) itemView.findViewById(R.id.iv_item_area_star);
            tvItemAreaContent = (TextView) itemView.findViewById(R.id.tv_item_area_content);
        }
    }

}
